var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require("connect-flash");
var session = require('express-session');
var MongoStore = require("connect-mongo")(session);
var multer = require('multer');

var intro = require('./routes/intro');
var users = require('./routes/users');
var index = require('./routes/index');
var admin = require('./routes/admin');
var manage = require('./routes/manage');
var logout = require('./routes/logout');
var postLoad = require('./routes/postLoad');
var postP = require('./routes/postP');
var getAll = require('./routes/getAll');
var article = require('./routes/article');
var addComment = require('./routes/addComment');
var getComments = require('./routes/getComments');
var getPage = require('./routes/getPage.js');
var getCommentsNumForAId = require('./routes/getCommentsNumForAId');
var updateP = require('./routes/updateP');
var deleteP = require('./routes/deleteP');
var search = require('./routes/search');
var getAllComments = require('./routes/getAllComments');
var deleteC = require('./routes/deleteC');



var settings = require('./settings');

var app = express();



// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
// app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(flash());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({
  secret:settings.cookieSecret,
  key: settings.db,//cookie name
  cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
  store: new MongoStore({
    db:settings.db,
    host:settings.host,
    port:settings.port
  })
}));
app.use(multer({
  dest:'./public/upload',
  rename:function(fieldname,filename){
    return filename;
  }
}));

app.use('/', intro);
app.use('/users', users);
app.use('/index', index);
app.use('/admin',admin);
app.use('/manage',manage);
app.use('/logout',logout);
app.use('/postLoad',postLoad);
app.use('/postP',postP);
app.use('/getAll',getAll);
app.use('/article',article);
app.use('/addComment',addComment);
app.use('/getComments',getComments);
app.use('/getPage',getPage);
app.use('/getCommentsNumForAId',getCommentsNumForAId);
app.use('/getAllComments',getAllComments);
app.use('/updateP',updateP);
app.use('/deleteP',deleteP);
app.use('/search',search);
app.use('/deleteC',deleteC);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;
  next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err
    });
  });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}
  });
});


module.exports = app;
